package com.itgone.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.itgone.common.Const;
import com.squareup.picasso.Picasso;

public class ProductImageVPAdapter extends PagerAdapter {
    private String[] imgUrls;
    private Context context;

    public ProductImageVPAdapter(Context context, String[] imgUrls) {
        this.imgUrls = imgUrls;
        this.context = context;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //container  容器  相当于用来存放imageView
        //从集合中获得图片
        int newPosition = position % imgUrls.length; //数组中总共有5张图片，超过数组长度时，取摸，防止下标越界
        ImageView imageView = new ImageView(context);
        Picasso.with(context).load(Const.URL_IMAGE_BASE + imgUrls[newPosition]).into(imageView);
        //把图片添加到container中
        container.addView(imageView);
        //把图片返回给框架，用来缓存
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }


    @Override
    public int getCount() {
        return imgUrls.length;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
}
